<?php

class create extends BaseAction {

	public function _default(){
		$filestructure = array(
				'data/'=>array('expdb','canonical/','exported/','index'),
				'doc/'=>array('hypothesis','abstract','introduction','methods','results','discussion','logbook','messages.log'),
				'graphs/',
				'scripts/',
				'studies/',
				'views/',
				'config.exp',
				'.exp'
			);
		
		$name = isset($GLOBALS['argv'][2]) ? $GLOBALS['argv'][2] : 'exp';
		if(is_dir($name)) _exit(1);
		echo verbose_message("== Making directory: $name");
		mkdir($name);
		chdir($name);
		self::create_files($filestructure);
		chdir('../');
	}
	
	public function create_files($filestructure){
		if(!is_array($filestructure)) return;
		foreach($filestructure as $name=>$contents){
			if(is_numeric($name)) $name = $contents;
			if(strpos($name,'/')!=false){
				echo verbose_message("== Making directory: $name");
				mkdir($name);
				if(isset($contents)){
					chdir($name);
					self::create_files($contents);
					chdir('../');
				}
			} else {
				echo verbose_message("=== Creating file: $name");
				if(file_exists("{$GLOBALS['basepath']}skeletons/$name.tpl")){
					$skel = file_get_contents("{$GLOBALS['basepath']}skeletons/$name.tpl");
					file_put_contents($name, $skel);
				} else {
					touch($name);
				}
			}
		}
		return;
	}
	
	public function view(){
		$name = isset($GLOBALS['argv'][2]) ? $GLOBALS['argv'][2] : 'new_view';
		echo verbose_message("== Creating view: $name");
		$skel = file_get_contents("{$GLOBALS['basepath']}skeletons/view.tpl");
		$skel = str_replace('@@@NEW_VIEW@@@',$name,$skel);
		file_put_contents("views/$name.php", $skel);
		echo verbose_message("== View skeleton $name created successfully");
	}
	
	public function study(){
		$name = isset($GLOBALS['argv'][2]) ? $GLOBALS['argv'][2] : 'new_study';
		echo verbose_message("== Creating study: $name");
		$skel = file_get_contents("{$GLOBALS['basepath']}skeletons/study.tpl");
		file_put_contents("studies/$name.php", $skel);
		echo verbose_message("== Study skeleton $name created successfully");
	}
	
	public function graph(){
		$name = isset($GLOBALS['argv'][2]) ? $GLOBALS['argv'][2] : 'new_graph';
		echo verbose_message("== Creating graph: $name");
		$skel = file_get_contents("{$GLOBALS['basepath']}skeletons/graph.tpl");
		file_put_contents("graphs/$name.php", $skel);
		echo verbose_message("== Graph skeleton $name created successfully");
	}
}